Register-Lib PowerShell.Collections -InitDependencies PowerShell.Objects, PowerShell.Types

# .SYNOPSIS
# Simple function to create a new ArrayList

# .DESCRIPTION
# This is an alternative to @(), useful when you want to maintain a reference to a List object 
# rather than rely on PowerShell's built in object[] handling.
function New-List {
    new-object System.Collections.ArrayList
}

# .SYNOPSIS
# Simple predicate that returns true if the pipeline input contains any of the supplied values
function ContainsAny($values) {
    $exists = $false;
    $input | % { 
        if($values -contains $_){
            $exists = $true
        }
    }
    return $exists;
}

<# 
.SYNOPSIS
Convert the pipeline input to an Array

.NOTES
From http://blogs.msdn.com/powershell/archive/2009/02/27/converting-to-array.aspx
#>
function ToArray
{
  begin
  {
    $output = @(); 
  }
  process
  {
    $output += $_; 
  }
  end
  {
    return ,$output; 
  }
}

<#
.SYNOPSIS
Returns true if the supplied item is Enumerable

.INPUT
Pipeline input is not valid for this function, as it interferes with the 
detection of a valid IEnumerable

#>
function IsEnumerable {
    param(        
        $item
    )    
    if(!$item) {        
        if($input){
            return $true
        }
    } else {
        return (
            (!($item -is [string])) -and
            $item -is [System.Collections.IEnumerable]
        )
    }
}

function Test-IsEnumerable {
    (1,2,3 | IsEnumerable) -eq $true
    (1 | IsEnumerable) -eq $false
    (IsEnumerable 1) -eq $false
    (IsEnumerable 1,2,3) -eq $True
    (IsEnumerable @{ Hash='This is a hash' }) -eq $True
    (IsEnumerable { write-host 'this is a code block' }) -eq $False
} 
# Test-IsEnumerable

<#
.SYNOPSIS
Returns true if the given item is a valid IDictionary 
#>
function IsDictionary {
    param(        
        [Parameter(ValueFromPipeline=$true)]
        $item
    )  
    if(!$item) {
        $item = $input
    }
    return ($item -is [System.Collections.IDictionary])
}
set-alias IsHash IsDictionary

function Test-IsDictionary {

    IsDictionary @{Test='YesItIs'}
    @{Test='YesItIs'} | IsDictionary 
    @{Test='YesItIs'}, @{Test='YesItIs'} | IsDictionary 
    @{Test='YesItIs'} | % {$_} | IsDictionary
    !(@{Test='YesItIs'} | % {$_.Key} | IsDictionary)
    !(123 | IsDictionary)
    !(IsDictionary 123)

}
#Test-IsDictionary

<#
.SYNOPSIS
Given an IEnumerable, invokes GetEnumerator in order to enumerate its internal items

.DESCRIPTION
This function is useful for enumerating the entries in an IDictionary
#>
function Loop-Enumerable {
    param(        
        [Parameter(ValueFromPipeline=$true)]
        $item
    ) 
    if($item){
        $item.GetEnumerator()
    }
}
set-alias Loop-Item Loop-Enumerable
set-alias Loop-Hash Loop-Enumerable

<#
.SYNOPSIS
Instantiates a new HashSet<T> where T is the -Type

.INPUT
Accepts as pipeline input a set of items to add to the new HashSet (duplicates will be ignored)
#>
function New-HashSet(
	# The collection type.  Defaults to System.Object
	$Type='System.Object'	
){
    $set = New-GenericObject -typeName System.Collections.Generic.HashSet -typeParameters $type
	$input | IgnoreEmpty | % { $set.Add($_) | out-null }
	,$set
}

